<template>
  <n-modal :show="state.dialogFormVisible" @onUpdateShow="close">
    <div class="coll-dia flex">
      <div class="left">
        <img class="cover" :src="state.cover" alt="" />
        <div class="names">{{ state.title }}</div>
      </div>
      <div class="right">
        <div class="nav">
          <div class="title">收藏</div>
          <div class="close" @click="close">
            <i class="landscape landscape-guanbi"></i>
          </div>
        </div>
        <div class="search">
          <n-input
            style="
              --n-color: #f5f5f5;
              --n-color-focus: #f5f5f5;
              --n-height: 46px;
            "
            v-model:value="state.keyword"
            placeholder="搜索或创建收藏夹"
            class="input"
          >
            <template #prefix>
              <i class="landscape landscape-sousuo"></i>
            </template>
          </n-input>
        </div>
        <div class="coll-list">
          <div class="coll-cell" v-for="item in showList" :key="item.id">
            <template v-if="item.cover">
              <img class="coll-cover" :src="item.cover" alt="" />
            </template>
            <template v-else>
              <div class="logo">
                <img src="@/assets/icon/collJia.png" alt="" />
              </div>
            </template>
            <div class="name u-line-1">{{ item.name }}</div>
            <n-button type="primary" class="btn" @click="handColl(item.id)">
              收藏
            </n-button>
            <!--<n-button type="default" class="btn">已收藏</n-button>-->
          </div>
        </div>
        <div class="create" @click="handAdd()">
          <img src="@/assets/icon/add.png" alt="" />
          <span>创建收藏夹</span>
        </div>
      </div>
    </div>
  </n-modal>
  <add-favorites ref="addRef" @success="getList" />
</template>
<script setup lang="ts">
import { NModal, NInput, NButton, useMessage } from 'naive-ui'
import { reactive, ref, defineAsyncComponent, computed } from 'vue'
import workApi from '@/service/api/work'
import detailApi from '@/service/api/detail'
const AddFavorites = defineAsyncComponent(() => import('./AddFavorites.vue'))
const emit = defineEmits(['success'])
const addRef = ref()
const toast = useMessage()
const handAdd = () => {
  addRef.value.open()
}
interface listItem {
  id: number
  name: string
  cover: string
  description: string
  coll_count: number
}
const handColl = async (work_id: number | string) => {
  const { code, msg } = await detailApi.coll({
    id: state.id,
    work_id: work_id
  })
  if (code !== 0) {
    toast.error(msg)
    return
  }
  toast.success('收藏成功')
  emit('success', state.id)
  close()
}
const tableData = ref<listItem[]>([])
const showList = computed(() => {
  if (!state.keyword) {
    return tableData.value
  }
  return tableData.value.filter((el: listItem) => {
    return el.name.includes(state.keyword)
  })
})
const getList = async () => {
  const { data } = await workApi.list()
  tableData.value = data
}
const state = reactive({
  dialogFormVisible: false,
  keyword: '',
  id: '',
  title: '',
  cover: ''
})
const close = () => {
  state.dialogFormVisible = false
}
const open = (row: any) => {
  state.id = row.id
  state.title = row.name
  state.cover = row.cover
  state.keyword = ''
  getList()
  state.dialogFormVisible = true
}
defineExpose({
  open
})
</script>

<style scoped lang="scss">
.coll-dia {
  width: 720px;
  background: #fff;
  border-radius: 15px;
  box-sizing: border-box;
  overflow: hidden;
  .left {
    background: #f5f5f5;
    padding: 32px;
    .cover {
      width: 248px;
      height: 188px;
      border-radius: 12px;
      object-fit: cover;
    }
    .names {
      margin-top: 12px;
      min-height: 95px;
      background: #ffffff;
      border-radius: 12px;
      border: 1px solid #e2e2e2;
      padding: 12px 13px;
    }
  }
  .right {
    flex: 1;
    padding: 24px;
    .nav {
      position: relative;
      .title {
        font-size: 20px;
        font-family: PingFang SC, PingFang SC;
        font-weight: bold;
        color: #333333;
        text-align: center;
      }
      .close {
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
        cursor: pointer;
      }
    }
    .search {
      margin-top: 24px;
      .landscape-sousuo {
        color: #999;
        font-size: 17px;
        margin-right: 8px;
      }
    }
    .coll-list {
      margin-top: 8px;
      height: 400px;
      overflow-y: auto;
      .coll-detault,
      .coll-cell {
        display: flex;
        align-items: center;
        padding: 12px 0;
        .logo {
          width: 48px;
          height: 48px;
          background: #eeeeee;
          border-radius: 6px;
          display: flex;
          align-items: center;
          justify-content: center;
          img {
            width: 24px;
            height: 24px;
          }
        }
        .coll-cover {
          width: 48px;
          height: 48px;
          border-radius: 6px;
          object-fit: cover;
        }
        .name {
          flex: 1 0 0;
          width: 0;
          margin: 0 12px;
          font-size: 14px;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          color: #333333;
        }
        .btn {
          font-size: 14px;
          width: 80px;
          border-radius: 8px;
        }
      }
    }
    .create {
      border-top: 1px solid #eeeeee;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 24px 0 0 0;
      cursor: pointer;
      img {
        width: 20px;
        height: 20px;
      }
      span {
        margin-left: 8px;
        font-size: 16px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #333333;
      }
    }
  }
}
</style>
<style lang="scss">
.coll-dia {
  .n-input .n-input__input-el {
    height: 46px;
    line-height: 46px;
  }
}
</style>
